package com.example.stlink.utils.activityUtil;

import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;

import com.example.stlink.R;
import com.example.stlink.configs.adapter.ViewPagerAdapter;

import java.util.List;

public class CheckCourseSignUtil {

    private AppCompatActivity appCompatActivity;
    private ViewPager2 viewPager2; // 视图
    private MenuItem menuItem; //

    private TextView tvSignList;
    private TextView tvCourseSignStatus;
    private TextView tvCourseSignLeave;

    public CheckCourseSignUtil(View view, AppCompatActivity appCompatActivity){
        this.appCompatActivity = appCompatActivity;
        initData(view);
    }

    private void initData(View view){
        // 视图翻页工具
        viewPager2 = view.findViewById(R.id.vp_course_sign_list);

        tvSignList = view.findViewById(R.id.tv_sign_list);
        tvCourseSignStatus = view.findViewById(R.id.tv_course_sign_status);
        tvCourseSignLeave = view.findViewById(R.id.tv_course_sign_leave);
        tvSignList.setClickable(true);
        tvCourseSignLeave.setClickable(true);
        tvSignList.setFocusable(true);
        tvCourseSignLeave.setFocusable(true);

    }

    /**
     * 将碎片与ViewPager2进行绑定，并监听viewPager2的滑动
     */
    public void initPage(List<Fragment> list){
        // 设置点击触发
        tvSignList.setOnClickListener(v ->
                viewPager2.setCurrentItem(0));
        tvCourseSignLeave.setOnClickListener(v ->
                viewPager2.setCurrentItem(2));
        // 注册页面改变的回调函数
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {  //  声明页面切换后的回调函数
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
            @Override
            public void onPageSelected(int position) { //根据滑动设置被选择的碎片
                switch (position){
                    case 0:{
                        tvSignList.setSelected(true);
                        tvCourseSignStatus.setText("已签到");
                        tvCourseSignLeave.setSelected(false);
                        break;
                    }
                    case 1:{
                        tvSignList.setSelected(true);
                        tvCourseSignStatus.setText("未签到");
                        tvCourseSignLeave.setSelected(false);
                        break;
                    }
                    case 2:{
                        tvSignList.setSelected(false);
                        tvCourseSignLeave.setSelected(true);
                        break;
                    }
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) { }
        });
        setupViewPager(viewPager2, list);
    }

    /**
     * 将碎片与viewPager2进行绑定
     * @param viewPager2 碎片集中管理类
     * @param list 碎片列表
     */
    private void setupViewPager(ViewPager2 viewPager2, List<Fragment> list) {
        FragmentManager fragmentManager = appCompatActivity.getSupportFragmentManager();
        // 将创建的碎片列表加入适配器中
        ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager, appCompatActivity.getLifecycle(), list);
        // viewPager2设置适配器
        viewPager2.setAdapter(adapter);
        viewPager2.requestDisallowInterceptTouchEvent(true);
    }

}
